package com.cskaoyan.gateway.controller.shopping;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.commons.tool.utils.CookieUtil;
import com.mall.order.OrderQueryService;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.dto.OneOrderDetailRequest;
import com.mall.order.dto.OneOrderDetailResponse;
import com.mall.shopping.ICartService;
import com.mall.shopping.IProductCateService;
import com.mall.commons.tool.exception.ValidateException;
import com.mall.shopping.IProductService;
import com.mall.shopping.bo.ShoppingGoodsBo;
import com.mall.shopping.bo.UpdateCartNumBO;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.dto.*;
import com.mall.user.annotation.Anoymous;
import com.mall.user.constants.SysRetCodeConstants;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/shopping")
public class ShoppingController {

    @Reference(check = false)
    IProductCateService productCateService;

    @Reference(check = false)
    IProductService productService;

    @Reference(check = false)
    ICartService cartService;

    @Reference(check = false)
    ICartService iCartService;


    @GetMapping("categories")
    @Anoymous
    public ResponseData categories(String sort) {
        AllProductCateRequest allProductCateRequest = new AllProductCateRequest();
        allProductCateRequest.setSort(sort);
        AllProductCateResponse response = productCateService.getAllProductCate(allProductCateRequest);
        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response.getProductCateDtoList());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    /**
     * 查看商品详情
     * @param id
     * @return
     */
    @GetMapping("/product/{id}")
    @Anoymous
    public ResponseData getProductDetail(@PathVariable Long id){
        ProductDetailRequest request = new ProductDetailRequest();
        request.setId(id);
        try {
            request.requestCheck();
        } catch (ValidateException e){
            return new ResponseUtil().setErrorMsg(e.getMessage());
        }
        ProductDetailResponse response = productService.getProductDetail(request);
        if (!response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setErrorMsg(response.getMsg());
        }
        ProductDetailDto productDetailDto = response.getProductDetailDto();
        return new ResponseUtil().setData(productDetailDto);
    }


    /**
     * 添加商品到购物车
     * @param request
     * @return
     */
    @PostMapping("/carts")
    public ResponseData addGoodsToCart(@RequestBody AddCartRequest request){
        try{
            request.requestCheck();
        } catch (ValidateException e) {
            return new ResponseUtil().setErrorMsg(ShoppingRetCode.REQUISITE_PARAMETER_NOT_EXIST.getMessage());
        }
        AddCartResponse addCartResponse = cartService.addToCart(request);
        if (!addCartResponse.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setErrorMsg(addCartResponse.getMsg());
        }
        return new ResponseUtil().setData(addCartResponse.getMsg());
    }

    /**
     * 获得购物车列表
     * @param request
     * @return
     */
    @GetMapping("carts")
    public ResponseData getCartsList(HttpServletRequest request){
        String userInfo = (String) request.getAttribute("userInfo");
        JSONObject jsonObject = JSON.parseObject(userInfo);
        String username = jsonObject.get("username").toString();
        CartListByIdRequest cartListByIdRequest = new CartListByIdRequest();
        cartListByIdRequest.setUsername(username);
        CartListByIdResponse response = cartService.getCartListByUsername(cartListByIdRequest);
        if (SysRetCodeConstants.SYSTEM_ERROR.getCode().equals(response.getCode())) {
            return new ResponseUtil().setErrorMsg(Integer.parseInt(response.getCode()),
                    response.getMsg());
        }
        return new ResponseUtil().setData(response.getCartProductDtos());
    }

    /**
     * 更新购物车中的商品数量和check情况
     * @param updateCartNumBO
     * @return
     */
    @PutMapping("carts")
    public ResponseData UpdateCartNum(@RequestBody UpdateCartNumBO updateCartNumBO) {
        UpdateCartNumRequest request = new UpdateCartNumRequest();
        request.setChecked(updateCartNumBO.getChecked());
        request.setItemId(updateCartNumBO.getProductId());
        request.setNum(updateCartNumBO.getProductNum());
        request.setUserId(updateCartNumBO.getUserId());
        UpdateCartNumResponse response = iCartService.updateCartNum(request);
        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<String>().setData("成功");
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }


    @DeleteMapping("carts/{uid}/{pid}")
    public ResponseData deleteCarts(@PathVariable("uid") long uid,@PathVariable("pid") long pid){
        DeleteCartItemRequest request=new DeleteCartItemRequest();
        request.setItemId(pid);
        request.setUserId(uid);
        try{
            request.requestCheck();
        }catch (ValidateException e){
            return new ResponseUtil<>().setErrorMsg(e.getMessage());
        }
        DeleteCartItemResponse response=cartService.deleteCartItem(request);
        if(!response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())){
            return new ResponseUtil<>().setErrorMsg(response.getMsg());
        }
        return new ResponseUtil<>().setData(response.getMsg());
    }

    @DeleteMapping("items/{id}")
    public ResponseData items(@PathVariable("id") long id){
        DeleteCheckedItemRequest request=new DeleteCheckedItemRequest();
        request.setUserId(id);
        DeleteCheckedItemResposne response=cartService.deleteCheckedItem(request);
        if(!response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())){
            return new ResponseUtil<>().setErrorMsg(response.getMsg());
        }
        return new ResponseUtil<>().setData(response.getMsg());

    }

    /**
     * 分页查询商品
     * @param productRequest
     * @return
     */
    @GetMapping("goods")
    @Anoymous
    public ResponseData goods(AllProductRequest productRequest) {
        if (productRequest == null) {
            return new ResponseUtil().setErrorMsg(Integer.parseInt(ShoppingRetCode.REQUISITE_PARAMETER_NOT_EXIST.getCode())
                    , ShoppingRetCode.REQUISITE_PARAMETER_NOT_EXIST.getMessage());
        }
        productRequest.requestCheck();
        AllProductResponse allProduct = productService.getAllProduct(productRequest);
        AllProductResponse2 response = new AllProductResponse2();
        response.setData(allProduct.getProductDtoList());
        response.setTotal(allProduct.getTotal());
        return new ResponseUtil().setData(response);
    }

    @GetMapping("recommend")
    @Anoymous
    public ResponseData getRecommend(){
        RecommendResponse response = productService.getRecommendGoods();
        if (SysRetCodeConstants.SUCCESS.getCode().equals(response.getCode())) {
            return new ResponseUtil().setData(response.getPanelContentItemDtos());
        }
        return new ResponseUtil().setErrorMsg(
                Integer.parseInt(response.getCode()), response.getMsg());
    }



}
